#include "CMap.h"

CMap::CMap(CSound *sManager,int idWorld)
{
	if (idWorld==4)
	{
		_loadGame=true;
	}
	else
		_loadGame=false;
	_world=idWorld;
	_isGameOver=false;
	_soundMangager=sManager;
	//Khoi tao QuadTree;
	_quadTree=NULL;
	//Doi Tuong hieu ung
	//BackGround
	CBackGround::InitResoure();
	//Items
	//Inform
	//Flighter
	LoadNewWorld();
	//Khoi tao dan;
	//Gan doi tuong infor
}
CMap::~CMap()
{
	delete _quadTree;
	delete _myScreen;
}
void CMap::LoadNewWorld()
{
	char *filename="\Maps/FlighterMap.map";
	if(filename!=NULL)
		LoadDataFromFile(filename);
}
void CMap::LoadDataFromFile(char* filePath)
{
	ifstream f(filePath);
	vector<string> itemsInfo;
	if(f.is_open())
	{
		string line;
		int id;
		int topCamera;
		int kind;
		int left, top;
		int hidden;
		int level;
		//_____________________DOC THONG SO VE MAP_______________________
		getline(f,line);
		getline(f,line);
		itemsInfo=SplitString(line,' ');//cat chuoi
		_mapWidth=atoi(itemsInfo.at(0).c_str());//Lay chieu rong 
		_mapHeight=atoi(itemsInfo.at(1).c_str());//lay chieu cao
		_world=atoi(itemsInfo.at(2).c_str());//Lay world
		topCamera=atoi(itemsInfo.at(3).c_str());//top camera
		_quadTree=new QuadTree(_mapWidth,_mapHeight);//Khoi tao quadtree 
		_myScreen=new CScreen(topCamera);
		getline(f,line);
		getline(f,line);
		itemsInfo=SplitString(line,' ');//cat
		level=atoi(itemsInfo.at(0).c_str());
		left=atoi(itemsInfo.at(1).c_str());
		top=atoi(itemsInfo.at(2).c_str());
		//khoi tao cho doi tuong info
		//khoi tao cho doi tuong flighter
		//__________________DOC THONG TIN CAC DOI TUONG___________________
		while(1)
		{
			Object *Ob=NULL;//Khoi tao doi tuong
			getline(f,line);
			itemsInfo=SplitString(line,' ');
			if(strcmp(line.c_str(), "[End]") == 0)//Ket thuc khi gap End
				break;
			if(strcmp(itemsInfo.at(0).c_str(), "[Obj]") == 0)//bo qua neu la dong tieu de
				continue;
			id=atoi(itemsInfo.at(0).c_str());//lay ID
			kind= atoi(itemsInfo.at(1).c_str());
			left =atoi(itemsInfo.at(2).c_str());
			top=atoi(itemsInfo.at(3).c_str());
			hidden=atoi(itemsInfo.at(4).c_str());
			switch(id)
			{
			case 3:
				Ob=new CWeapon(left,top,_myScreen,_quadTree,kind,_soundMangager);
				break;
			}
			if (Ob!=NULL)
			{
				_quadTree->Add(_quadTree->_pRoot,Ob);
			}
			
		}	
	}
}
void CMap::Reset()
{
	_isGameOver=false;
	_myScreen->Reset();
	LoadNewWorld();
	//Reset bulletManager
}
void CMap::FinshMap()
{

}
void CMap::Update(DWORD tickPerFrame,CDxInput* input)
{
	_quadTree->UpdatePosition(_quadTree->_pRoot);
	_myScreen->MoveCamera(-1);
}
void CMap::Draw()
{
	//Ve ra nhung doi tuong trong man hinh
	_quadTree->DrawObjects(_myScreen->Camera.top,_myScreen->Camera);
	//update fighter
	//update thong so
	if (_isGameOver)
	{
		//ket thuc
	}
}
//Tao canh vat trong game
void CMap:: CreateSighMap(int world)
{

}
//Ham cat chuoi
vector<string> CMap::SplitString(std::string str, char ch)
{
	vector<string> result;
	int n = str.size();
	string line = "";
	for(int i = 0; i < n; i++){
		while((i<n)&&(str.at(i)!=ch))
		{
			line.push_back(str.at(i));
			i++;
		}
		result.push_back(line);
		line.clear();		
	}
	return result;
}